package org.example.judge_admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.judge_pojo.User;

public interface UserMapper extends BaseMapper<User> {

    @Update("update tb_user set rating = rating + #{rating} where id = #{userId}")
    boolean addRatingById(String userId,Integer rating);


    /**
     * 获取指定用户排名,执行之前需调用setRank(0)
     * @param id
     * @return
     */
    @Select("" +
            "select `ran` from (SELECT " +
            "@rant := @rant + 1 AS `ran`,`id` " +
            "FROM (SELECT `id` FROM `tb_user` ORDER BY `rating` DESC) `a`) `b` " +
            "where `id` = #{id}")
    Integer getUserRatingRank(String id);
    @Select("SET @rant = #{rank}")
    void setRank(Integer rank);
}
